<template>
  <el-dialog v-loading="loading" :append-to-body="true" :close-on-click-modal="false" :visible.sync="visable" :title="title" width="500px" @close="closeDlg">
    <el-form ref="xform" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="上级" prop="parent.name">
        <el-input v-model="form.parent.name" :disabled="true" />
      </el-form-item>
      <el-form-item :label="codeLabel" prop="code">
        <el-input v-model="form.code" autocomplete="false" maxlength="40" />
      </el-form-item>
      <el-form-item :label="nameLabel" prop="name">
        <el-input v-model="form.name" autocomplete="false" maxlength="100" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" autocomplete="off" disabled>
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { get } from '@/api/department'
import { resetCache } from '@/api/basedata'
export default {
  name: 'DeptForm',
  data() {
    return {
      isAdd: false,
      loading: false,
      visable: false,
      codeLabel: '编号',
      nameLabel: '名称',
      title: '',
      typeOptions: [{ id: 1, name: '公司' }, { id: 2, name: '部门' }],
      form: {
        name: '',
        type: '',
        code: '',
        parent: {
          id: null,
          name: ''
        }
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入编号', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetFields() {
      this.form = {
        id: null,
        name: '',
        code: '',
        prentId: null,
        parent: {
          name: '',
          id: null
        }

      }
    },
    cancel() {
      this.closeDlg()
    },
    doSubmit() {
      this.$refs['xform'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
          // --
          resetCache('department')
        }
      })
    },
    addTop() {
      this.resetFields()
      this.form.parentId = 0
      this.form.parent = {}
      this.form.type = 1
      this.form.parent.name = ''
      this.isAdd = true
      this.visable = true
      this.title = '新增一级公司'
      this.codeLabel = '公司编号'
      this.nameLabel = '公司名称'
    },
    add(node, type) {
      this.resetFields()
      this.form.parentId = node.id
      this.form.parent = {}
      this.form.type = type
      this.form.parent.name = node.label
      this.isAdd = true
      this.visable = true

      if (type === 1) {
        this.title = '新增公司'
        this.codeLabel = '公司编号'
        this.nameLabel = '公司名称'
      } else {
        this.title = '新增部门'
        this.codeLabel = '部门编号'
        this.nameLabel = '部门名称'
      }
    },
    edit(id) {
      get(id).then(data => {
        this.form.id = data.id
        this.form.name = data.name
        this.form.code = data.code
        this.form.type = data.type
        this.form.parentId = data.parentId
        if (data.parent) {
          this.form.parent = data.parent
        } else {
          this.form.parent = {}
        }
        if (data.type === 1) {
          this.title = '编辑公司'
          this.codeLabel = '公司编号'
          this.nameLabel = '公司名称'
        } else {
          this.title = '编辑部门'
          this.codeLabel = '部门编号'
          this.nameLabel = '部门名称'
        }

        this.isAdd = false
        this.visable = true
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    doAdd() {
      request({ url: '/sys/department/add', method: 'post', data: this.form }).then(res => {
        this.closeDlg()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.getList() // refrsh
      }).catch(err => {
        this.loading = false
        this.$message.error('添加错误。' + err.message)
      })
    },
    doEdit() {
      request({ url: '/sys/department/update', method: 'post', data: this.form }).then(res => {
        this.closeDlg()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.getList()
      }).catch(err => {
        this.loading = false
        this.$message.error('修改错误。' + err.message)
      })
    },
    closeDlg() {
      this.visable = false
      this.resetFields()
    }
  }
}
</script>
